﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadPoolTest
{
    class Program
    {
        static int i = 0;

        #region 测试1

        //static void Main(string[] args)
        //{
        //    //假设有100个请求线程
        //    int num = 100;

        //    //使用 MutipleThreadResetEvent
        //    using (var countdown = new MutipleThreadResetEvent(num))
        //    {
        //        for (int i = 0; i < num; i++)
        //        {
        //            //开启N个线程，传递MutipleThreadResetEvent对象给子线程
        //            ThreadPool.QueueUserWorkItem(MyHttpRequest, countdown);
        //        }
        //        //等待所有线程执行完毕
        //        countdown.WaitAll();
        //    }

        //    Console.WriteLine("所有的网络请求以及完毕，可以继续下面的分析...");
        //    Console.ReadKey();
        //}

        ///// <summary>
        ///// 假设的网络请求
        ///// </summary>
        ///// <param name="state">参数</param>
        //private static void MyHttpRequest(object state)
        //{
        //    // Thread.Sleep(1000);
        //    Console.WriteLine(String.Format("请求完成:{0}", ++i));

        //    MutipleThreadResetEvent countdown = state as MutipleThreadResetEvent;
        //    //发送信号量 本线程执行完毕
        //    countdown.SetOne();
        //}
        #endregion

        #region 测试2
        static void Main(string[] args)
        {
            for (int index = 0; index < 100; index++)
                ThreadPool.QueueUserWorkItem(CallBack,index.ToString());

            Console.WriteLine("*****************");
            Console.ReadKey();

            Thread.Sleep(5000);
        }

        private static void CallBack(object state)
        {
            string indexStr = state as string;

            //Thread.Sleep(1000);
            //Console.WriteLine(String.Format("请求完成:{0}", ++i));
            Console.WriteLine("请求完成:" + indexStr);
            //while (true)
            //{
            //    Thread.Sleep(100);
            //    Console.WriteLine(Convert.ToString(state));
            //}
        }
        #endregion
    }





}
